package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlansDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlans;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description NursingPlanServiceImpl
 * @Author lwh
 * @Date 2025-05-10  15:36
 */
@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;


    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlanVo> getAllNursingPlans() {
        return nursingPlanMapper.selectAllPlans();
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto
     */
    @Transactional
    @Override
    public void add(NursingPlanDto nursingPlanDto) {
        // 将基本信息的DTO转为pojo
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        // 插入护理计划其他信息
        nursingPlan.setStatus(1);
        nursingPlan.setCreateTime(nursingPlan.getCreateTime());
        nursingPlanMapper.insert(nursingPlan);

        // 抽离出护理计划信息
        List<NursingProjectPlansDto> nursingProjectPlans = nursingPlanDto.getProjectPlans();
        // 将护理计划Dto转为POJO
        List<NursingProjectPlans> nursingProjectPlansPoJo = BeanUtil.copyToList(nursingProjectPlans, NursingProjectPlans.class);
        // 插入护理计划
        nursingProjectPlansPoJo.forEach(n -> {
            // 将插入的护理计划返回的id，赋值给护理计划-项目表
            n.setPlanId(nursingPlan.getId());
            n.setCreateTime(nursingPlan.getCreateTime());
            n.setCreateBy(nursingPlan.getCreateBy());
            // 插入护理计划-项目
            nursingProjectPlanMapper.insertProjectPlans(n);
        });

    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> page(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> result = nursingPlanMapper.page(name, status);
        PageResponse<NursingPlanVo> nursingPlanVos = PageResponse.of(result, NursingPlanVo.class);
        // 插入count
        nursingPlanVos.getRecords().forEach(n -> {
            n.setCount(nursingPlanMapper.count(n.getId()));
        });
        return nursingPlanVos;
    }


    /**
     * 根据id查询护理计划
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        nursingPlanVo.setProjectPlans(nursingProjectPlanMapper.selectProjectPlans(id));
        return nursingPlanVo;
    }

    /**
     * 修改护理计划
     *
     * @param id
     * @param nursingPlanDto
     */
    @Transactional
    @Override
    public void update(Long id, NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setUpdateTime(LocalDateTime.now());
        // 修改护理计划表
        nursingPlanMapper.update(nursingPlan);
        // 修改护理计划-项目表
        //1.删除原有的数据
        nursingProjectPlanMapper.deleteProjectPlans(id);
        //2.插入新的数据
        List<NursingProjectPlansDto> projectPlans = nursingPlanDto.getProjectPlans();
        List<NursingProjectPlans> nursingProjectPlans = BeanUtil.copyToList(projectPlans, NursingProjectPlans.class);
        nursingProjectPlans.forEach(n -> {
            n.setPlanId(nursingPlan.getId());
            n.setUpdateTime(nursingPlan.getCreateTime());
            n.setUpdateBy(nursingPlan.getUpdateBy());
            // 插入护理计划-项目
            nursingProjectPlanMapper.insertProjectPlans(n);
        });
    }

    /**
     * 删除护理计划
     *
     * @param id
     */
    @Transactional
    @Override
    public void delete(Long id) {
        // 先删除护理计划-项目表中的数据
        nursingProjectPlanMapper.deleteProjectPlans(id);
        // 删除护理计划
        nursingPlanMapper.delete(id);
    }

    /**
     * 修改护理计划状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id, status);
    }
}
